package com.cvic.se.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cvic.se.entity.Department;
import com.cvic.se.service.DepartmentService;
import com.cvic.se.utils.Result;

import io.swagger.annotations.ApiOperation;


/**
 * <p>
 * InnoDB free: 4096 kB; (`MECH_ID`) REFER `test/mechanism`(`MECH_ID`) 前端控制器
 * </p>
 *
 * @author Qin,Zhao
 * @since 2021-07-21
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    
	@Autowired
	DepartmentService departmentService;
	
	/**
	 * @category 根据机构编号获取该机构下的全部的部门信息
	 * @param instId 机构编号
	 * @return 
	 */
	@GetMapping("/getAll")
	@ApiOperation(value = "根据机构编号获取该机构下的全部的部门信息")
	public Result getAllDept(String instId) {
		return departmentService.getAllDept(instId);
	}
	
	/**
	 * @category 插入一个新的部门信息 
	 * @param dept 部门对象
	 * @return
	 */
	@PostMapping("/insert")
	@ApiOperation(value = "插入一个新的部门信息")
	public Result insertDept(@RequestBody Department dept) {
		return departmentService.insertDept(dept);
	}
	
	/**
	 * @category 删除一个部门
	 * @param deptId 部门编号
	 * @return 判断删除部门操作是否成功
	 */
	@DeleteMapping("/delete")
	@ApiOperation(value = "删除一个部门")
	public Result deleteDept(Integer deptId) {
		return departmentService.deleteDept(deptId);
	}
	
	/**
	 * @category 更新部门信息
	 * @param dept 部门对象
	 * @return 判断更新部门操作是否成功
	 */
	@PostMapping("update")
	@ApiOperation(value = "更新部门信息")
	public Result updateDept(@RequestBody Department dept) {
		return departmentService.updateDept(dept);	
	}
	
	/**
	 * @category 分页查询部门信息
	 * @param current 页码
	 * @param limit 一个页面展示的部门信息数
	 * @return total 
	 * @return records
	 */
	@GetMapping("pageDept/{current}/{limit}")
	@ApiOperation(value = "分页查询")
	public Result pageDepartmentInfo(@PathVariable long current,@PathVariable long limit) {
		Page<Department> pageDept = new Page<>(current, limit);
		departmentService.page(pageDept, null);
		
		long total = pageDept.getTotal();//总记录数
		List<Department> records = pageDept.getRecords();//数据list集合
		return Result.success().data("total",total).data("rows",records);
	}
}
